package com.leador.http;

import android.content.Context;

import com.leador.api.navi.model.LeadorNaviLocation;
import com.leador.listener.OnPointListener;
import com.leador.map.api.location.MapLocation;
import com.leador.module.ILocation;
import com.leador.module.SerEntity;
import com.leador.module.TraceLocation;
import com.leador.module.request.Entity;
import com.leador.module.request.Fence;
import com.leador.module.request.Historyalarm;
import com.leador.module.request.Track;
import com.leador.module.request.TrackRequest;

import java.io.File;

/**
 * Created by liuha on 2016/9/18.
 */
public class HttpManager {
    private static int ency = 0;
    private static HttpManager INSTANCE = null;
    public static OnPointListener onPointListener = null;
    private HttpManager(){

    }
    public static HttpManager getInstance(){
        if(INSTANCE == null){
            synchronized (HttpManager.class){
                if(INSTANCE  == null){
                    INSTANCE = new HttpManager();
                }
            }
        }
        return INSTANCE;
    }

    public void setEncy(int ency) {
        this.ency = ency;
    }

    /**
     * 1.新增服务ID
     * @param context
     * @param serEntity
     */
    public void addServiceId(Context context, SerEntity serEntity) {
        RequestParams.addServiceId(context,serEntity,ency);
    }

    public  void addEntity(Context context, Entity entity) {
        RequestParams.addEntity(context,entity,ency);
    }

    public  void updateEntity(Context context,Entity entity) {
        RequestParams.updateEntity(context,entity,ency);
    }

    public  void deleteEntity(Context context,Entity entity){
        RequestParams.deleteEntity(context,entity,ency);
    }

    public  void listEntity(Context context,Entity entity){
        RequestParams.listEntity(context,entity,ency);
    }

    public  void addcolumn(Context context,Entity entity){
        RequestParams.addcolumn(context,entity,ency);
    }
    public  void listcolumn(Context context,Entity entity){
        RequestParams.listcolumn(context,entity,ency);
    }
    public  void deletecolumn (Context context,Entity entity){
        RequestParams.deletecolumn(context,entity,ency);
    }
    public  void gethistory(Context context, TrackRequest trackRequest){
        RequestParams.gethistory(context,trackRequest,ency);
    }
    public  void addcolumn(Context context, Track track){
        RequestParams.addcolumn(context,track,ency);
    }
    public  void listcolumn(Context context,Track track){
        RequestParams.listcolumn(context,track,ency);
    }
    public  void deletecolumn(Context context,Track track){
        RequestParams.deletecolumn(context,track,ency);
    }
    public  void createFence(Context context, Fence fence){
        RequestParams.createFence(context,fence,ency);
    }
    public  void updateFence(Context context, Fence fence){
        RequestParams.updateFence(context,fence,ency);
    }
    public  void deleteFence(Context context,int serviceId,int fenceId){
        RequestParams.deleteFence(context,serviceId,fenceId,ency);
    }
    public  void getListFence(Context context, long serviceId, String fenceIds,String creator){
        RequestParams.getListFence(context,serviceId,fenceIds,creator,ency);
    }
    public  void queryMonitoredStatus(Context context,long serviceId,String monitoredPersons,int fenceId){
        RequestParams.queryMonitoredStatus(context,serviceId,monitoredPersons,fenceId,ency);
    }
    public  void queryFenceHistoryAlarmInfo(Context context,Historyalarm historyalarm){
        RequestParams.queryFenceHistoryAlarmInfo(context,historyalarm,ency);
    }
    public void addTracks(Context context,String serviceId,String entityName,File file,OnPointListener listener){
        if(onPointListener == null){
            onPointListener = listener;
        }
        RequestParams.addTracks(context,ency,serviceId,entityName,file);
    }

    public void addTrack(Context context, ILocation iLocation,OnPointListener listener){
        if(onPointListener == null){
            onPointListener = listener;
        }
        RequestParams.addTrack(context,iLocation,ency);
    }


    public static TraceLocation queryRealtimeLoc(MapLocation mapLocation) {
        ILocation iLocation = new ILocation();
        iLocation.setLoc_time(String.valueOf(mapLocation.getTime()));
        iLocation.setLongitude(String.valueOf(mapLocation.getLongitude()));
        iLocation.setLatitude(String.valueOf(mapLocation.getLatitude()));
        iLocation.setSpeed(String.valueOf(mapLocation.getSpeed()));
        iLocation.setAccuracy((int) mapLocation.getAccuracy());
        iLocation.setBearing((int) mapLocation.getBearing());
        TraceLocation traceLocation = new TraceLocation();
        //speed
        String speed = iLocation.getSpeed();
        if(speed == null){
            speed = "0";
        }
        traceLocation.setSpeed(Float.valueOf(speed));
        //time
        String locTime = iLocation.getLoc_time();
        if(locTime == null){
            locTime="0";
        }
        traceLocation.setTime(locTime);
        //longitude
        String longitude = iLocation.getLongitude();
        if(longitude == null){
            longitude = "0";
        }
        traceLocation.setLongitude(Double.parseDouble(longitude));
        //latitude
        String latitude = iLocation.getLatitude();
        if(latitude == null){
            latitude = "0";
        }
        traceLocation.setLatitude(Double.parseDouble(latitude));
        //bear
        int bear = iLocation.getBearing();
        if(bear == 0){
            bear = 0;
        }
//        traceLocation.set
        int altitude = iLocation.getAccuracy();
        traceLocation.setAltitude(altitude);
        return traceLocation;
    }
}
